package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.mingtu.common.view.MultiGridView;
import com.mingtu.jgspatrol.R;


public class BasicLayerAdapter extends BaseAdapter {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private int[] layer = {R.drawable.selector_checkbox_layer_basic2, R.drawable.selector_checkbox_layer_basic3,
            R.drawable.selector_checkbox_layer_basic4, R.drawable.selector_checkbox_layer_basic5};

    //    R.drawable.selector_checkbox_layer_basic1,
    private MultiGridView multiGridView;

    public BasicLayerAdapter(Context context, MultiGridView multiGridView) {
        this.context = context;
        this.multiGridView = multiGridView;
    }

    @Override
    public int getCount() {
        return layer.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_layer_basic, null);

            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            holder.checkBox.setBackgroundResource(layer[position]);

//            ViewGroup.LayoutParams lp = holder.checkBox.getLayoutParams();
//            int screenWidth = ScreenUtils.getScreenWidth(); //一行5个，间隙是10dp
//            int mWidth = (screenWidth - SizeUtils.dp2px(60)) / 4;
//            int mHeight = mWidth * SizeUtils.dp2px(30) / SizeUtils.dp2px(78);
//            lp.width = mWidth;
//            lp.height = mHeight;
//            holder.checkBox.setLayoutParams(lp);

            holder.checkBox.setTag("checkbox" + position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ViewHolder finalHolder = holder;
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) multiGridView.findViewWithTag("checkbox" + position);
                boolean isChecked = checkBox.isChecked();
                if (mOnItemClickListener != null) {
                    //注意这里使用tag方法获取position
                    if (isChecked) {
                        finalHolder.checkBox.setChecked(false);
                    } else {
                        finalHolder.checkBox.setChecked(true);
                    }
                    mOnItemClickListener.onItemClick(v, position + 1, !isChecked);
                }
            }
        });

        return convertView;
    }


    class ViewHolder {
        public LinearLayout layout;
        public CheckBox checkBox;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position, boolean isChecked);
    }

}
